home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / PERMUTE.CPP < prev    next >
Text File  |  1997-05-06  |  2KB  |  62 lines

  1.  #include <numeric>    // For accumulate.
  2.  #include <vector>     // For vector.
  3.  #include <functional> // For less.
  4.  
  5.  using namespace std;
  6.  
  7.  int main ()
  8.  {
  9.    //
  10.    // Initialize a vector using an array of integers.
  11.    //
  12.    int  a1[] = {0,0,0,0,1,0,0,0,0,0};
  13.    char a2[] = "abcdefghji";
  14.    //
  15.    // Create the initial set and copies for permuting.
  16.    //
  17.    vector<int>  m1(a1+0, a1+10);
  18.    vector<int>  prev_m1((size_t)10), next_m1((size_t)10);
  19.    vector<char> m2(a2+0, a2+10);
  20.    vector<char> prev_m2((size_t)10), next_m2((size_t)10);
  21.  
  22.    copy(m1.begin(), m1.end(), prev_m1.begin());
  23.    copy(m1.begin(), m1.end(), next_m1.begin());
  24.    copy(m2.begin(), m2.end(), prev_m2.begin());
  25.    copy(m2.begin(), m2.end(), next_m2.begin());
  26.    //
  27.    // Create permutations.
  28.    //
  29.    prev_permutation(prev_m1.begin(), prev_m1.end(), less<int>());
  30.    next_permutation(next_m1.begin(), next_m1.end(), less<int>());
  31.    prev_permutation(prev_m2.begin(), prev_m2.end(), less<int>());
  32.    next_permutation(next_m2.begin(), next_m2.end(), less<int>());
  33.    //
  34.    // Output results.
  35.    //
  36.    cout << "Example 1: " << endl << "     ";
  37.    cout << "Original values:      ";
  38.    copy(m1.begin(), m1.end(), ostream_iterator<int>(cout," "));
  39.    cout << endl << "     ";
  40.    cout << "Previous permutation: ";
  41.    copy(prev_m1.begin(), prev_m1.end(), ostream_iterator<int>(cout," "));
  42.  
  43.    cout << endl<< "     ";
  44.    cout << "Next Permutation:     ";
  45.    copy(next_m1.begin(), next_m1.end(), ostream_iterator<int>(cout," "));
  46.    cout << endl << endl;
  47.  
  48.    cout << "Example 2: " << endl << "     ";
  49.    cout << "Original values: ";
  50.    copy(m2.begin(), m2.end(), ostream_iterator<char>(cout," "));
  51.    cout << endl << "     ";
  52.    cout << "Previous Permutation: ";
  53.    copy(prev_m2.begin(), prev_m2.end(), ostream_iterator<char>(cout," "));
  54.    cout << endl << "     ";
  55.  
  56.    cout << "Next Permutation:     ";
  57.    copy(next_m2.begin(), next_m2.end(), ostream_iterator<char>(cout," "));
  58.    cout << endl << endl;
  59.  
  60.    return 0;
  61.  }
  62.